package com.yjps.house.service;

import com.yjps.house.dao.HistoryDao;
import com.yjps.house.dao.HouseDao;
import com.yjps.house.entity.HouseHistory;
import com.yjps.house.model.House;
import com.yjps.house.model.PublishHistory;
import com.yjps.system.dao.UserDao;
import com.yjps.system.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("publishService")
public class PublishService {

    @Autowired
    private HouseDao houseDao;

    @Autowired
    private HistoryDao historyDao;

    @Autowired
    private UserDao userDao;

    /**
     * 保存房源(未审核)和发布记录
     * @param house
     * @param agentsId
     * @param publishTime
     * @return houseId
     */
    @Transactional
    public int publishHouse(House house,String agentsId,String publishTime) {
        //未审核状态
        house.setState(0);
        //未认证
        house.setIsAuthentication(0);
        //非精品
        house.setIsBoutique(0);
        int houseId = houseDao.save(house);

        //保存发布记录
        PublishHistory history = new PublishHistory(Integer.parseInt(agentsId), houseId, publishTime);
        int historyId = historyDao.save(history);

        return houseId;
    }

    /**
     * 获取该经纪人所有房源发布记录
     * @param agentsId
     * @return
     */
    @Transactional
    public List<HouseHistory> allListByAgentsId(int agentsId, int startRow, int pageSize){
        //查询经纪人房源发布记录
        String hql="from t_house_publish_history p where p.agents_id =: agentsId and valid := isValid";
        Map<String, Object> map = new HashMap<>();
        map.put("agentsId", agentsId);
        map.put("valid",1);
        List<PublishHistory> histories=historyDao.find(hql, startRow, pageSize);

        List<HouseHistory> ps=new ArrayList<>();
        //封装成Publish
        for (PublishHistory ph:histories){
            //查询记录对应的house
            House house=houseDao.get(ph.getHouseId());
            if (house!=null){
                String hql2="from t_user u where u.id in (select user_id from t_agents a where a.id =: agentsId )";
                Map<String,Object> map2=new HashMap<>();
                map2.put("agentsId",agentsId);
                List<User> users=userDao.find(hql2, map);
                if (!users.isEmpty()){
                    HouseHistory p=new HouseHistory(agentsId,users.get(0).getUsername(),ph.getTime(),house);
                    ps.add(p);
                }

            }
        }
        return ps;
    }

    /**
     * 获取该经纪人待审核的房源发布记录
     * @param agentsId
     * @param startRow
     * @param pageSize
     * @return
     */
    @Transactional
    public List<HouseHistory> unVerfiyListByAgentsId(int agentsId,int startRow,int pageSize){
        List<HouseHistory> ps= allListByAgentsId(agentsId, startRow, pageSize);
        for (HouseHistory p : ps ){
            if (p.getHouse().getState()== 0){
                ps.remove(p);
            }
        }
        return ps;
    }

    /**
     * 获取该经纪人已审核的房源发布记录
     * @param agentsId
     * @param startRow
     * @param pageSize
     * @return
     */
    @Transactional
    public List<HouseHistory> verfiyListByAgentsId(int agentsId,int startRow,int pageSize){
        List<HouseHistory> ps= allListByAgentsId(agentsId, startRow, pageSize);
        for (HouseHistory p : ps ){
            if (p.getHouse().getState()== 1){
                ps.remove(p);
            }
        }
        return ps;
    }

    /**
     * 取消房源发布
     * @param houseId
     */
    @Transactional
    public void cancelPublish(int houseId){
        //无效房源
        House house=houseDao.get(houseId);
        houseDao.invalid(house);
        if (house.getState()==1){
            //房源已审,发布的记录不用无效化
            return;
        }
        //无效发布记录
        String hql="from t_house_publish_history p where h.house_id =: houseId";
        Map<String,Object> map=new HashMap<>();
        map.put("houseId",houseId);
        PublishHistory ph= (PublishHistory)historyDao.find(hql,map).get(0);
        historyDao.invalid(ph);
    }


    //--------这是一条分割线------------
    //以下为门店经理


    /**
     * 获取门店下所有的房源发布记录
     * @param shopId
     * @return
     */
    @Transactional
    public List<HouseHistory> allListByShopId(int shopId,int startRow,int pageSize){
        //查询该门店下所有经纪人的发布记录
        String hql="from t_house_publish_history p" +
                "where p.agentsId IN(select a.id from t_agents a where a.shop_id := shopId) and p.valid := isValid";
        Map<String, Object> map = new HashMap<>();
        map.put("shopId",shopId);
        map.put("valid",1);
        List<PublishHistory> histories=historyDao.find(hql,map);

        //查询记录对应的房源
        List<HouseHistory> ps=new ArrayList<>();
        for(PublishHistory ph:histories){
            House house=houseDao.get(ph.getHouseId());
            if (house!=null){
                String hql2="from t_user u where u.id in (select user_id from t_agents a where a.id =: agentsId )";
                Map<String,Object> map2=new HashMap<>();
                map2.put("agentsId",ph.getAgentsId());
                List<User> users=userDao.find(hql2, map);
                if (!users.isEmpty()){
                    HouseHistory p=new HouseHistory(ph.getAgentsId(),users.get(0).getUsername(),ph.getTime(),house);
                    ps.add(p);
                }
            }
        }
        return ps.subList(startRow-1,startRow+pageSize-1);
    }


    /**
     * 获取门店下未通过审核的房源发布记录
     * @param shopId
     * @param startRow
     * @param pageSize
     * @return
     */
    @Transactional
    public List<HouseHistory> unVerfiyListByShopId(int shopId,int startRow,int pageSize){
        List<HouseHistory> ps=allListByShopId(shopId, startRow, pageSize);
        for (HouseHistory p : ps ){
            if (p.getHouse().getState()== 0){
                ps.remove(p);
            }
        }
        return ps;
    }

    /**
     * 获取门店下已通过审核的房源发布记录
     * @param shopId
     * @param startRow
     * @param pageSize
     * @return
     */
    @Transactional
    public List<HouseHistory> verfiyListByShopId(int shopId,int startRow,int pageSize){
        List<HouseHistory> ps=allListByShopId(shopId, startRow, pageSize);
        for (HouseHistory p : ps ){
            if (p.getHouse().getState()== 1){
                ps.remove(p);
            }
        }
        return ps;
    }

}
